package com.test.douyin.live;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.base.HttpUtils;

public class GetHtmlData {
    private static void createAndShowGUI() {
        int gap = 10;
        JFrame jFrame = new JFrame("获取抖音直播间m3u8,flv视频推流");
        jFrame.setSize(600, 400);
        jFrame.setLocation(600, 200);
        jFrame.setLayout(null);
        JPanel pInput = new JPanel();
        pInput.setBounds(gap, gap, 565, 120);
        pInput.setLayout(new GridLayout(4, 3, gap, gap));
        JLabel id = new JLabel("直播分享URL");
        JTextField locationText = new JTextField();
        JButton button = new JButton("解析一下");
        pInput.add(id);
        pInput.add(locationText);
        // 文本域
        JTextArea jTextArea = new JTextArea();
        jTextArea.setLineWrap(true);
        jTextArea.setBounds(gap, 150, 565, 200);
        jTextArea.setText("打开抖音直播->分享->复制地址；如：https://v.douyin.com/eddQr6T/");
        button.setBounds(gap, 80, 565, 30);
        jFrame.add(pInput);
        jFrame.add(button);
        jFrame.add(jTextArea);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jFrame.setVisible(true);
        // 鼠标监听
        button.addActionListener(new ActionListener() {
            boolean checkedpass = true;

            public void actionPerformed(ActionEvent e) {
                checkedpass = true;
                checkEmpty(locationText, "直播分享URL");
                String location = locationText.getText();
                Set<String> set = fun(location);
                if (checkedpass) {
                    jTextArea.setText("");
                    set.forEach(s -> {
                        jTextArea.append(s + "\r\n");
                    });
                }
            }

            // 检验是否为空
            private void checkEmpty(JTextField tf, String msg) {
                if (!checkedpass)
                    return;
                String value = tf.getText();
                if (value.length() == 0) {
                    JOptionPane.showMessageDialog(jFrame, msg + " 不能为空");
                    tf.grabFocus();
                    checkedpass = false;
                }
            }
        });
    }

    public static void main(String[] args) throws SocketTimeoutException, IOException {
        // 显示应用 GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static Set<String> fun(String id) {
        try {
            String data = HttpUtils.getInstance().getForm(id);
            String[] dataArr = data.split("http");
            Set<String> set = new LinkedHashSet<>();
            for (String s : dataArr) {
                if (!s.startsWith("s://") && !s.startsWith("://")) {
                    continue;
                }
                if (s.indexOf("image") != -1 || s.indexOf("jpeg") != -1 || s.indexOf("douyinpic") != -1 || s.indexOf("pstatp") != -1) {
                    continue;
                }
                String dataUrl = "http" + s;
                if (dataUrl.indexOf("complete_push_urls") != -1) {
                    break;
                }
                dataUrl = dataUrl.split("\",")[0];
                dataUrl = dataUrl.split("\\\\")[0];
                dataUrl = dataUrl.split("\"}")[0];
                dataUrl = dataUrl.split("\"]")[0];
                set.add(dataUrl.split("[?]")[0]);
            }
            return set;
        } catch (IOException e) {
            return null;
        }

    }
}
